package lms.dwr.demo.web.component.security;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import lms.dwr.demo.common.Constants;
import lms.dwr.demo.model.ModelUser;

import org.apache.log4j.Logger;

public class UserAuthenticationFilter 
implements Filter 
{
	
	/**
	 * The LOGGER
	 */
	private static final Logger LOGGER = Logger.getLogger(UserAuthenticationFilter.class);
	
	/**
	 * The filter configuration
	 */
	private FilterConfig config = null;
	
	/**
	 * The error page
	 */
	private String pageError;
	
	/**
	 * The login page 
	 */
	private String pageLogin;
	
	/**
	 * The access denied page
	 */
	private String pageAccessDenied;
	

	@Override
	public void init(FilterConfig config) throws ServletException 
	{
		this.config = config;
		
		this.pageError = config.getInitParameter("pageError");
		this.pageLogin = config.getInitParameter("pageLogin");
		this.pageAccessDenied = config.getInitParameter("pageAccessDenied");
		
	}
	
	@Override
	public void destroy() 
	{
		config = null;
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException 
	{
		
		HttpServletRequest hreq  = (HttpServletRequest)request;
		HttpServletResponse hrsp = (HttpServletResponse)response;
		HttpSession session = hreq.getSession();
		
		String host = this.getHost(hreq);
		
		String basePath = host + hreq.getContextPath();
		String currentPath = host + hreq.getRequestURI() + (hreq.getQueryString() != null ? "?" + hreq.getQueryString() : "");
		
		ModelUser account = (ModelUser) session.getAttribute(Constants.DefaultUsrParamSessionHolder);
				
		if(account == null )
		{
			hrsp.sendRedirect(basePath + this.pageLogin + 
					(hreq.getHeader("REFERER") != null ? ("&from=" + hreq.getHeader("REFERER")) : ("&from=" + currentPath)));
		}
		else
		{
			chain.doFilter(request, response);
		}
		
	}
	
	/**
	 * Obtains the host URI with the HTTP servlet request
	 * 
	 * @param request
	 *          the HTTP servlet request
	 * @return the host URI
	 */
	private String getHost(HttpServletRequest request) 
	{
		return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
	}

	public FilterConfig getConfig() {
		return config;
	}

	public void setConfig(FilterConfig config) {
		this.config = config;
	}

	public static Logger getLogger()
	{
		return LOGGER;
	}

	public String getPageError()
	{
		return pageError;
	}

	public void setPageError(String pageError)
	{
		this.pageError = pageError;
	}

	public String getPageLogin()
	{
		return pageLogin;
	}

	public void setPageLogin(String pageLogin)
	{
		this.pageLogin = pageLogin;
	}

	public String getPageAccessDenied()
	{
		return pageAccessDenied;
	}

	public void setPageAccessDenied(String pageAccessDenied)
	{
		this.pageAccessDenied = pageAccessDenied;
	}

}
